<!-- 技术咨询需求 -->
<script setup lang="ts">
import {
  getConsultationPage,
  getConsultationForm,
  addConsultation,
  updateConsultation,
  deleteConsultation,
} from "@/api/tecConsult/index";
import {
  ConsultationQuery,
  ConsultationPageVO,
  ConsultationForm,
} from "@/api/tecConsult/types";

const queryFormRef = ref(ElForm); //查询表单
const conFormRef = ref(ElForm); //技术咨询表单

const viewDialog = ref(false); //判断是否为查看
const loading = ref(false); //加载状态
const queryParams = reactive<ConsultationQuery>({
  page: 1,
  pageSize: 10,
});

const startTime = ref("");
const endTime = ref("");
const total = ref(0); // 数据总数
const pageData = ref<ConsultationPageVO[]>(); // 用户分页数据
watch(startTime, (newVal) => {
  if (newVal) {
    queryParams.startTime = newVal;
  }
});
watch(endTime, (newVal) => {
  if (newVal) {
    queryParams.endTime = newVal;
  }
});

// 弹窗对象
const dialog = reactive({
  visible: false,
  type: "Consultation-form",
  width: 800,
  title: "",
});

// 技术咨询表单数据
const formData = reactive<ConsultationForm>({});

const rules = reactive({
  demand: [{ required: true, message: "请输入技术问题", trigger: "blur" }],
  project_brief: [
    { required: true, message: "请输入项目简介", trigger: "blur" },
  ],
  question: [{ required: true, message: "请输入技术问题", trigger: "blur" }],
  expectation: [
    { required: true, message: "请输入期望解决方案", trigger: "blur" },
  ],
  create_by: [{ required: true, message: "请输入咨询人", trigger: "blur" }],
  create_date: [{ required: true, message: "请输入咨询时间", trigger: "blur" }],
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  getConsultationPage(queryParams)
    .then(({ data }) => {
      pageData.value = data.items;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  startTime.value = "";
  endTime.value = "";
  queryParams.page = 1;
  queryParams.startTime = undefined;
  queryParams.endTime = undefined;
  handleQuery();
}

/**
 * 打开弹窗
 */
function openDialog(type: string, id?: string) {
  dialog.visible = true;
  dialog.type = type;

  if (dialog.type === "Consultation-form") {
    // 表单弹窗
    if (id) {
      dialog.title = "修改用户";
      getConsultationForm(id).then(({ data }) => {
        Object.assign(formData, { ...data });
      });
    } else {
      dialog.title = "新增用户";
    }
  } else if (dialog.type === "Consultation-view" && id) {
    // 查看弹窗
    getConsultationForm(id).then(({ data }) => {
      Object.assign(formData, { ...data });
    });
    viewDialog.value = true;
    dialog.title = "用户信息";
  }
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  viewDialog.value = false;
  formData.id = undefined;
  conFormRef.value.resetFields();
  conFormRef.value.clearValidate();
}

/** 表单提交 */
const handleSubmit = useThrottleFn(() => {
  if (dialog.type === "Consultation-form") {
    conFormRef.value.validate((valid: any) => {
      if (valid) {
        const userId = formData.id;
        loading.value = true;
        if (userId) {
          ElMessageBox.confirm("确认修改管理员？", "警告", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning",
          }).then(function () {
            console.log("test");
            updateConsultation(formData)
              .then(() => {
                ElMessage.success("修改管理员成功");
                closeDialog();
                resetQuery();
              })
              .finally(() => (loading.value = false));
          });
        } else {
          ElMessageBox.confirm("确认添加管理员？", "警告", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning",
          }).then(function () {
            addConsultation(formData)
              .then(() => {
                ElMessage.success("新增管理员成功");
                closeDialog();
                resetQuery();
              })
              .finally(() => (loading.value = false));
          });
        }
      }
    });
  }
}, 3000);

/** 删除用户 */
function handleDelete(id?: string) {
  if (id) {
    ElMessageBox.confirm("确认删除用户?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      loading.value = true;
      deleteConsultation(id)
        .then(() => {
          ElMessage.success("删除成功");
          resetQuery();
        })
        .finally(() => (loading.value = false));
    });
  }
}
onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search-container">
      <el-form
        :inline="true"
        :model="queryParams"
        ref="queryFormRef"
        label-width="100px"
        class="form-container"
      >
        <el-form-item label="咨询人" prop="create_by">
          <el-input
            placeholder="咨询人"
            v-model="queryParams.create_by"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <!-- <el-form-item label="咨询时间" prop="create_date">
          <el-date-picker
            v-model="startTime"
            class="!w-[200px]"
            type="datetime"
            placeholder="开始时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
          <span class="time">~</span>
          <el-date-picker
            v-model="endTime"
            class="!w-[200px]"
            type="datetime"
            placeholder="截止时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item> -->
        <el-form-item class="handleQuery">
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery">
            <i-ep-refresh />
            重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" @click="openDialog('Consultation-form')"
          ><i-ep-plus />新增</el-button
        >
      </template>

      <el-table v-loading="loading" :data="pageData" style="width: 100%" border>
        <el-table-column
          label="咨询人"
          align="center"
          prop="create_by"
          width="120"
        />
        <el-table-column label="技术需求" align="center" prop="demand" />
        <el-table-column
          label="咨询时间"
          align="center"
          prop="create_date"
          width="220"
        />

        <el-table-column label="操作" fixed="right" width="200" align="center">
          <!-- #default="scope" -->
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog('Consultation-view', scope.row.id)"
              ><i-ep-view />查看</el-button
            >

            <el-button
              type="primary"
              link
              size="small"
              @click="openDialog('Consultation-form', scope.row.id)"
              ><i-ep-edit />编辑</el-button
            >

            <el-button
              type="primary"
              link
              size="small"
              @click="handleDelete(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :width="dialog.width"
      @close="closeDialog"
      :title="dialog.title"
    >
      <el-form
        :model="formData"
        :rules="rules"
        label-width="110px"
        ref="conFormRef"
      >
        <el-form-item label="技术需求" prop="demand">
          <el-input
            type="textarea"
            placeholder="请输入技术问题"
            v-model="formData.demand"
            :disabled="viewDialog"
        /></el-form-item>
        <el-form-item label="项目简介" prop="project_brief">
          <el-input
            type="textarea"
            placeholder="请输入项目简介"
            v-model="formData.project_brief"
            :disabled="viewDialog"
        /></el-form-item>
        <el-form-item label="技术问题" prop="question">
          <el-input
            type="textarea"
            placeholder="请输入技术问题"
            v-model="formData.question"
            :disabled="viewDialog"
        /></el-form-item>
        <el-form-item label="期望解决方案" prop="expectation">
          <el-input
            type="textarea"
            placeholder="请输入期望解决方案"
            v-model="formData.expectation"
            :disabled="viewDialog"
        /></el-form-item>
        <!-- <el-form-item label="咨询时间" prop="create_date">
          <el-date-picker
            v-model="formData.create_date"
            type="datetime"
            placeholder="请选择咨询时间"
            value-format="YYYY-MM-DD HH:mm:ss"
            :disabled="viewDialog"
            class="!w-[100%]"
        /></el-form-item>
        <el-form-item label="咨询人" prop="create_by">
          <el-input
            type="textarea"
            placeholder="咨询人"
            v-model="formData.create_by"
            :disabled="viewDialog"
        /></el-form-item> -->
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 认</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

.form-container {
  display: flex;
  flex-wrap: wrap;
}

.handleQuery {
  margin-left: auto;
}

.time {
  margin: 0 10px;
}
</style>
